package com.biaobiaoqi.scoreanalysis.hibernate.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.biaobiaoqi.scoreanalysis.util.SchoolType;

@Entity
@Table
public class Clazz extends BaseBean {
	@Column(nullable = false)
	private Integer classNumber;
	@Column(nullable = false)
	private Integer beginYear;
	@ManyToOne
	@JoinColumn(nullable = false)
	private ClazzType clazzType;
	@ManyToOne
	private Teacher teacherMaster;
	@OneToMany(mappedBy = "belongClazz")
	private List<Student> students;
	@ManyToMany(mappedBy = "clazzs")
	private List<Examination> examinations = new ArrayList<Examination>();
	@Column(nullable = false)
	private SchoolType schoolType;
	@Transient
	private String clazzName;

	public SchoolType getSchoolType() {
		return schoolType;
	}

	public void setSchoolType(SchoolType schoolType) {
		this.schoolType = schoolType;
	}

	public Integer getClassNumber() {
		return classNumber;
	}

	public void setClassNumber(Integer classNumber) {
		this.classNumber = classNumber;
	}

	public Integer getBeginYear() {
		return beginYear;
	}

	public void setBeginYear(Integer beginYear) {
		this.beginYear = beginYear;
	}

	public ClazzType getClazzType() {
		return clazzType;
	}

	public void setClazzType(ClazzType type) {
		this.clazzType = type;
	}

	public Teacher getTeacherMaster() {
		return teacherMaster;
	}

	public void setTeacherMaster(Teacher teacherMaster) {
		this.teacherMaster = teacherMaster;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public String getClazzName() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR) - beginYear;
		int month = cal.get(Calendar.MONTH) + 1;

		if (year < 0 || (year == 0 && month < 9)) {
			return schoolType.getName() + beginYear + "级" + "未开学：" + "（"
					+ classNumber + "）班";
		} else if (year == 0 && month > 8 || year < 3 || year == 3 && month < 9) {
			return schoolType.getName() + year + "年级" + "（" + classNumber
					+ "）班";
		} else {
			switch (schoolType) {
			case PRIMARY_SCHOOL:
				if (year == 3 && month > 8 || year < 6 || year == 6
						&& month < 9) {
					return schoolType.getName() + year + "年级" + "（"
							+ classNumber + "）班";
				} else {
					return schoolType.getName() + beginYear + "级" + "已毕业："
							+ "（" + classNumber + "）班";
				}
			case SENIOR_SCHOOL:
			case HIGH_SCHOOL:
				return schoolType.getName() + beginYear + "级" + "已毕业：" + "（"
						+ classNumber + "）班";
			}
		}
		return clazzName;
	}

	public List<Examination> getExaminations() {
		return examinations;
	}

	public void setExaminations(List<Examination> examinations) {
		this.examinations = examinations;
	}
}
